package cn.walls1717.wallsmall.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.walls1717.wallsmall.common.pojo.to.MemberPrice;
import cn.walls1717.wallsmall.common.pojo.to.SkuReductionTO;
import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.Query;
import cn.walls1717.wallsmall.coupon.dao.SkuFullReductionDao;
import cn.walls1717.wallsmall.coupon.entity.MemberPriceEntity;
import cn.walls1717.wallsmall.coupon.entity.SkuFullReductionEntity;
import cn.walls1717.wallsmall.coupon.entity.SkuLadderEntity;
import cn.walls1717.wallsmall.coupon.service.MemberPriceService;
import cn.walls1717.wallsmall.coupon.service.SkuFullReductionService;
import cn.walls1717.wallsmall.coupon.service.SkuLadderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity>
    implements SkuFullReductionService {

    @Resource
    private SkuLadderService skuLadderService;
    @Resource
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page =
            this.page(new Query<SkuFullReductionEntity>().getPage(params), new QueryWrapper<SkuFullReductionEntity>());

        return new PageUtils(page);
    }

    /**
     * 保存sku阶梯价格，满减，会员价格信息 sms_sku_ladder sms_sku_full_reduction sms_member_price
     *
     *
     * @param skuReductionTO 保存参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSkuReduction(SkuReductionTO skuReductionTO) {
        // sku优惠、满减等信息 wallsmall-sms ->
        // sms_sku_ladder
        SkuLadderEntity skuLadder = new SkuLadderEntity();
        BeanUtil.copyProperties(skuReductionTO, skuLadder);
        skuLadder.setAddOther(skuReductionTO.getCountStatus());
        if (skuReductionTO.getFullCount() > 0) {
            skuLadderService.save(skuLadder);
        }

        // sms_sku_full_reduction
        SkuFullReductionEntity skuFullReduction = new SkuFullReductionEntity();
        BeanUtil.copyProperties(skuReductionTO, skuFullReduction);
        skuFullReduction.setAddOther(skuReductionTO.getCountStatus());
        if (skuFullReduction.getFullPrice().compareTo(new BigDecimal("0")) > 0) {
            save(skuFullReduction);
        }

        // sms_member_price
        List<MemberPrice> memberPrices = skuReductionTO.getMemberPrice();
        // 只有会员价格大于0的才保存会员价格
        List<MemberPriceEntity> memberPriceEntities = memberPrices.stream().map(memberPrice -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTO.getSkuId());
            // 会员等级id
            memberPriceEntity.setMemberLevelId(memberPrice.getId());
            // 会员等级名称
            memberPriceEntity.setMemberLevelName(memberPrice.getName());
            // 会员价格
            memberPriceEntity.setMemberPrice(memberPrice.getPrice());
            return memberPriceEntity;
        }).filter(memberPrice -> memberPrice.getMemberPrice().compareTo(new BigDecimal("0")) > 0)
            .collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }
}